/*********************************************************************************
 *      Copyright:  (C) 2019 Wu Yujun<540726307@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  module_main.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(2019年06月06日)
 *         Author:  Wu Yujun <540726307@qq.com>
 *      ChangeLog:  1, Release initial version on "2019年06月06日 19时46分42秒"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <pthread.h>



#include "server_socket.h"
#include "gsmd.h"
#include "comport.h"
#include "at_cmd.h"
#include "create_thread.h"
#include "ppp.h"



void print_usage(const char *program_name) 
{  
    printf("\n%s -- (2019.7.7)\n", program_name);  
    printf(" Usage: %s -p <server_port>  [-h <server_use_help>]\n", program_name);  
    printf("        -p --port       the server listen port\n") ;
    printf("        -h --help       the server file how to use\n");  
    printf("        -d --daemon     running in background\n");  

    return ;
}  

int g_stop = 0 ;
void sig_handler(int sig_num)
{
    if(sig_num == SIGUSR1)
    {
        g_stop = 1 ;
    }
}


int main(int argc, char **argv)
{
    char                *program_name = NULL ;
    int                 opt = -1 ;
    int                 port = 0 ;
    int                 daemon_run = 0 ;
    int                 rv = -1 ;
    pthread_t           ppp_id,sms_id,socket_id ;
    st_gsmd_ctx         *gsmd_ctx ;



    const char *short_opts = "p:h";                         //set  option
    program_name = basename(argv[0]) ;

    const struct option long_opts[] =       {  
        {"help", no_argument, NULL, 'h'},  
        { "port", required_argument, NULL, 'p'},  
        {"daemon", no_argument, NULL, 'd'},
        {0, 0, 0, 0}  
    };  

    while ((opt= getopt_long(argc, argv, short_opts, long_opts,NULL)) != -1) 
    {
        switch (opt) 
        {
            case 'p':
                port = atoi(optarg);
                break ;
            case 'd':
                daemon_run = 1 ;
                break ;
            case 'h':
                print_usage(argv[0]) ;
                return 0;
        }
    }
    if( !port )
    {
        print_usage(program_name);
        return 0;
    }
    

    gsmd_ctx = init_gprs_module(port) ;
    if(!gsmd_ctx)
    {
        printf("init_gprs_module() error\n") ;
        return -1 ;
    }

    gprsd_creat_thread(&ppp_id, ppp_thread, &sms_id, sendmess_thread,
            &socket_id, socket_thread, (void *)gsmd_ctx) ;

    /*   program running in backgrund     */
    if(daemon_run)
    {
        printf("%s running in background\n", program_name) ;
        daemon(0,0) ;
    }
    signal(SIGUSR1, sig_handler) ;
    while(!g_stop)
    {

        if(gsmd_ctx->gsmRegInfo.ready != OK)
        {
            rv = check_regsiter(Data_COM,gsmd_ctx) ; 
            if(rv < 0)
            {
                gsmd_sleep(rv) ;
            }
        }
        sleep(10) ;
    }

    destroy_gsmd(gsmd_ctx) ;

/* End Of Main */
    return 0 ;
}

